package net.jcip.examples;

public class ImutableCache<K,V> implements Cache<K,V>{
	
	private volatile Pair<K,V> keyVal;
	public ImutableCache(K key, V value){
		keyVal = new Pair<K, V>(key,value);
	}
	
	public V get( K key){
		Pair<K,V> keyValCopy = keyVal;
		if (keyValCopy.equals(key)){
			return keyValCopy.getValue();
		} else {
			return null;
		}
	}
	
	public void put(K key, V val){
		keyVal = new Pair<K,V>(key,val);
	}
	
}

class Pair<K,V>{
	private final K key;
	private final V value;
	
	Pair( K k, V v ){
		this.key = k;
		this.value = v;
	}

	public K getKey() {
		return key;
	}

	public V getValue() {
		return value;
	}
	
}